package com.Xht.www.servlet;

//后端查询获取钱包总余额的请求

import com.Xht.www.dao.UserWalletBalanceDAO;
import com.Xht.www.po.UserWalletBalancePO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/getUserWalletBalance")
public class GetUserWalletBalanceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        int userId = Integer.parseInt(request.getParameter("userId"));
        UserWalletBalanceDAO dao = new UserWalletBalanceDAO();
        UserWalletBalancePO walletBalance = dao.getUserByUserId(userId);

        if (walletBalance != null) {
            String json = "{\"totalAmount\": \"" + walletBalance.getTotalAmount() + "\", " +
                    "\"rechargeAmount\": \"" + walletBalance.getRechargeAmount() + "\", " +
                    "\"consumptionAmount\": \"" + walletBalance.getConsumptionAmount() + "\"}";
            out.print(json);
        } else {
            out.print("{}");
        }
        out.flush();
        out.close();
    }
}
